
//临时存储的一页的数据
var canvasData=[];
$(function(){
	
	//生成大的cavas
	$('#pagemain').on('click',".p_canvas_box",function(){
		//改变大的canvas 的布局及图片
		var index=$(this).index();
		if($(this).attr("title")=="封面"||$(this).attr("title")=="封底"||$(this).attr("title")=="扉页"){			
			$(".layout_change").addClass("out_layout_change");
			$(".editor_current").html($(this).attr("title"));
		}else{			
			$(".layout_change").removeClass("out_layout_change");
			//var pagenum=UtilProperty[workid].worker[1].page==-1?(index-1):index;	
			$(".editor_current").html('第'+$(this).attr("page")+'页');
		}
		$(this).addClass("editoring").siblings().removeClass("editoring");		
 		changeoriginalcanvas(index);		
	})
	
	
	//document.getElementById('originalcanvas').addEventListener("touchend", originalTouchEnd, false);
	 
	
	//保存编辑
	$('.editor_oper_con').on('click',".comfirm_change",function(){
		saveimageeditor();	
		
	})
	
	//取消编辑
	$('.editor_oper_con').on('click',".canace_change",function(){
		var cas=document.getElementById("reallycanvas");
		var cxt=cas.getContext("2d");
		cxt.clearRect(0,0,cas.width,cas.height);
		ctxmsg ={};
			$(".editor_oper_con").hide();
			$(".editor_view").show();
			
	})
	//旋转canvas中的图片
	$('.editor_oper_con').on('click',"#roat_btn",function(){
		var currentid=canvas_img.getAttribute("currentid");
		ctxmsg[currentid].transform=Number(ctxmsg[currentid].transform)
		ctxmsg[currentid].transform += 90;
		ctxmsg[currentid].ischange=true;
		canvasimgrote();
		
	})
	
	
	//替换
	$('.editor_oper_con').on('click',"#replace_btn",function(){
		$("#singerInput").attr("onfunction","1");
		$("#singerFiles .upload_img").removeClass("active_border");
		$('.up_page').fadeIn()
		$(".up_pagecontain").animate({bottom: '0'}, "slow");
		$(".singerInputmask").css("height",$('.up_page').height()-$(".up_pagecontain").height());
		
	})
	//关闭单页上传图片
	$('.up_page').on('click',".u_close,.singerInputmask",function(){		
		$('.up_page').fadeOut()
		$(".up_pagecontain").animate({bottom: '-100%'}, "slow");
		
	})
		
	//选择需要图片
	$('#singerFiles').on('click',"li",function(){		
		if($(this).children().hasClass("la-ball-beat")){
			$(".dialog_tips").show().find(".dialog_txt").text("正在上传，请稍后");	
			setTimeout(function(){$(".dialog_tips").hide()},1200)
		}else if ($(this).children().hasClass("up_refreshtxt")){		
			refleshUpload($(this));
			$(".dialog_tips").show().find(".dialog_txt").text("上传失败请重新上传");	
			setTimeout(function(){$(".dialog_tips").hide()},1200)
		}else{
			$("#singerFiles .upload_img").removeClass("active_border");
			$(this).find('.upload_img').addClass("active_border");
			var _flag=$("#singerInput").attr("onfunction");
			var imgurl=$(".active_border").attr("src");
			var image_id=$(".active_border").attr("image_id");
			if(_flag=="0"){	// 在当前页的控件中添加图片
				//改变 UtilProperty  中当前页的数据
				addimgaetocanvas(imgurl,image_id,$(this).attr("imgw"),$(this).attr("imgh"));
			
			}else if(_flag=="1"){//替换图片
				canvasimgreplace(imgurl,image_id,$(this).attr("imgw"),$(this).attr("imgh"));
				
			}	
			$('.up_page').fadeOut()
			$(".up_pagecontain").animate({bottom: '-100%'}, "slow");	
		}
		
	})
	
	
	//打开 布局版式调整
	$('.editor_view').on('click',".layout_change",function(){
		var pageno=$("#originalcanvas").attr("page");
		var _canvas=$("#pagemain .p_canvas_box").eq(pageno);
		if(pageno&&!(_canvas.attr("title")=="封面"||_canvas.attr("title")=="封底"||_canvas.attr("title")=="扉页")){
			$('.layoutcon').fadeIn()
			$(".layoutbox").animate({bottom: '0'}, "slow");
		}else{
			$(".dialog_tips").show().find(".dialog_txt").text("请先选择页面");	
			setTimeout(function(){$(".dialog_tips").hide()},1000)
		}
	})
	//关闭布局版式调整
	$('.layoutcon').on('click',".l_close,.layoutmask",function(){		
		$('.layoutcon').fadeOut()
		$(".layoutbox").animate({bottom: '-100%'}, "slow");
		
	})
	
	
	//选择布局并且更换布局
	$('.layoutlist').on('click',".layout_list",function(){	
		var pageno=$("#originalcanvas").attr("page");
		if(pageno){
			var index=$(this).closest('.layout_box').index();
			var is_change=judelayimg(pageno,index)//判断选择的布局中的图片控件是否少于当前页面中的图片 
			if(is_change){
				 $(".isyes").text("删除").attr({"oper":"changelayout","pageno":pageno,"index":index}).removeClass("default").addClass("primary").show();
				 dialogshow("提示",'您确定删除图片吗？');		 
				 $(".iscancel").text("取消").removeClass("primary").addClass("default");
			}else{
				//确定更改布局
	 			comfirmchangelayout(pageno,index);
			}
		}
	})
	
	
	//在编辑区域 选择需要调整的控件
	$('#editor_control_box').on('click',".editor_control_list ",function(){	
		$(this).addClass("editoring").siblings().removeClass("editoring");
		var currentid=$(this).attr("currentid");
		canvas_img.setAttribute("currentid",currentid);
		canvas_img.width=ctxmsg[currentid].w;
		canvas_img.height=ctxmsg[currentid].h;
		canvas_img.setAttribute("currentid",currentid);
		ctx_2d.clearRect(0,0,canvas_img.width,canvas_img.height);
		canvasdrag();
		$('#reallycanvas').closest(".canvasbg").css({"width":ctxmsg[currentid].w,"height":ctxmsg[currentid].h});
	})
	
	
	//保存并预览
	$('.editor_view').on('click',".page_save",function(){		
		$("#loadtoast").show();
		saveCanvasOper();
		$(".editorcon").hide();
		$(".previewcon").show();	
		$(".gotoedit").show();
		if(UtilProperty[workid].update_work_page.length>0){
			baseAjax('other/work/update_work_page','POST',{work_id:workid,data:JSON.stringify(UtilProperty[workid].update_work_page)},function(data){	
				$('.albumitem[work_id="'+workid+'"] .albumimg').find("img").attr("src",UtilProperty[workid].base64image[0].imgurl);
					//canvas保存图片成功后修改ischange的值
 					changeischange();
 					UtilProperty[workid].update_work_page=[];
			})
		}
		document.title='预览作品';
		$("#loadtoast").hide();
	})
	
	//单击 预览相册 页面的小图 查看大图
	$('.previewbox').on('click',".preview_element ",function(){
		$(".preview_element").removeClass("focus");
		$(this).addClass("focus");
		var imgsrc=$(this).find("img").attr("src");
		$(".large_page_view").find("img").attr("src",imgsrc);	
		var previewtitle;			
		var previewtitle=$(this).attr("title")=="封面"||$(this).attr("title")=="封底"||$(this).attr("title")=="扉页"?$(this).attr("title"):'第'+$(this).attr("page")+'页';
		$(".editor_preview").html(previewtitle);
	})
	
	//编辑
	$('.previewbox').on('click',".gotoedit ",function(){	
		$(".previewcon").hide();		
		$(".editorcon").show();	
		document.title='编辑作品';
	})
	
	
	//提交作品 作品页面更新	 
	$('.previewbox').on('click',".page_save ",function(){
		var isflag=checkwork()//检测作品中所有的图片控件是否都有填充了图片
		if(!isflag){
			//复制订单编号
			var order_sn = $.cookie('order_no');
			if(/^(E\d{23}|C\d{20})$/.test(order_sn)){
				$(".orderinput").val(order_sn);
			}
			
			$(".previewcon").hide();
			document.title='提交作品';
			$(".submitalbum").show();
			$(".gotoedit").hide();
		}else{
			dialogshow("提示","填充完整再提交！");	
		}
		$(".a_type").html(UtilProperty[workid].workerInfo.work_name);
		
		
	})
	
	//增加印制数量
	$('.submitalbum').on('click',".order_add ",function(){
		var valnum=parseInt($(".orderNumber").val())+1;
		$(".orderNumber").val(valnum);
		$('.order_jian').css("color","#838383");
	})
	
	$('.submitalbum').on('click',".order_jian ",function(){
		var valnum=parseInt($(".orderNumber").val());
		if(valnum>1){
			valnum--;
			$(".orderNumber").val(valnum);
		}else{
			$(this).css("color","#999");
		}
		
		
	})
	
	
	
	//提交作品 确认	
	$('.submitalbum').on('click',".comfirm_submit ",function(){
		var orderno=$(".orderinput").val();		
		if(orderno==""){
			$(".orderinputtips").html("请填写订单编号");
		}else{
			var oldstatus=UtilProperty[workid].workerInfo.status;
			var data = {work_id:workid,order_sn:orderno,status:oldstatus==0?1:oldstatus,work_number:$(".orderNumber").val()};
			if($(".albumname").val()!=""){
				data.name=$(".albumname").val();
			}
			baseAjax('other/work/update_work','POST',data,function(data){
				if(data.code==200){
					$(".orderinput").val('');
					dialogshow("提示",'提交成功');
					 $(".iscancel").attr("submitalbum","yes");
					 UtilProperty[workid].workerInfo.status=1;
					 var crrent=$('.albumlist[status="0"] .albumitem');
					 var time=$('.albumitem[work_id="'+workid+'"]').closest(".albumlist").attr("time");
					 var curhtml=$('.albumitem[work_id="'+workid+'"]').prop("outerHTML");
					 $('.albumitem[work_id="'+workid+'"]').remove();
					 if($('.albumlist[status="0"][time="'+time+'"] .albumcontain').html()==''){
					 	$('.albumlist[status="0"][time="'+time+'"]').remove();
					 	
					 }
					 if(!$(".albumlist").hasClass("submitedalbum")){
					 	var oldhtml='<div class="albumlist submitedalbum">'+
							'<div class="timefigure"><span class="figurecircle"></span></div>'+
							'<div class="timeline"><span></span></div>'+
							'<div class="albumtip">	已提交作品</div>'+											
						'</div>'+
						'<div class="albumlist" status="1" time="'+time+'">'+
							'<div class="timefigure"><span class="figurenum">'+time+'</span></div>'+
							'<div class="timeline"><span></span></div>'+							
							'<div class="albumcontain">'+
							curhtml+
							'</div>'+					
						'</div>';
					 	$(".albumend").before(oldhtml)
					 }else{
						 if($('.albumlist[status="1"][time="'+time+'"]').length==0){
						 	var ind=$(".submitedalbum").index()+1;
						 	var timeArr=[];
						 	for(var k=ind; k<($(".albumlist").length-1); k++){
						 		timeArr.push($(".albumlist").eq(k).attr("time"));						 		
						 	}
						 	timeArr.push(time);
						 	timeArr=timeArr.sort(sortNumber);
						 	var j=$.inArray(time,timeArr);						 	
						 	var newhtml='<div class="albumlist" status="1" time="'+time+'">'+
											'<div class="timefigure"><span class="figurenum">'+time+'</span></div>'+
											'<div class="timeline"><span></span></div>'+
											'<div class="albumcontain">'+
											curhtml+
											'</div>'+				
										'</div>';										
						 	$(".album .albumlist").eq(j+ind).after(newhtml);
						 }else{
						 	$('.albumlist[status="1"][time="'+time+'"] .albumcontain').append(curhtml);
						 }
					 }
					 
					 
				}
			})
		}
		
	})
	
	//返回	
	$('.submitalbum').on('click',".back_submit ",function(){
		if(UtilProperty[workid].workerInfo.status==0){
			$(".gotoedit").show();
		}if(UtilProperty[workid].workerInfo.status==1||UtilProperty[workid].workerInfo.status==2){
			$(".gotoedit").hide();
		}
		$(".previewcon").show();
		$(".submitalbum").hide();
	})
		
	//确定文字编辑	
	$('.editor_textview').on('click',".text_ok ",function(){
		var currentid=$(this).attr("currentid");
		//把编辑过的文字回填
		comfrimTextToCanvas(currentid);
		
	})
	
	//关闭布局版式调整
	$('.editor_textview').on('click',".text__close,.textviewInputmask",function(){		
		$('.editor_textview').fadeOut()
		$(".text_contain").animate({bottom: '0'}, "slow");	
		$(".text_input").val('');
		$(".textviewInputmask").css("height",$('.editor_textview').height()-$(".text_contain").height());
 	
		
	})
	
	
	
})

function creatPage(){
	var o_canvas= document.getElementById('originalcanvas')
	var o_cantxt=o_canvas.getContext("2d");
	o_cantxt.clearRect(0,0,o_canvas.width,o_canvas.height);
	var $obj=$("#pagemain");
	$("#pagemain").empty();
	$obj.attr("workerlistid",workid)
	var worker=UtilProperty[workid].worker;	
	UtilProperty[workid].base64image=new Array;
	
	for(var i=0; i<worker.length;i++){
		var item=worker[i];	
		//if(item.title!="扉页"){		
		var canvas=document.createElement("canvas");						
		var context=canvas.getContext("2d");
		var w=boxwindthheight.width;
		var h=boxwindthheight.height;
		var scale;		
		if(item.width / item.height > w / h) {
			scale=w/item.width;
		} else {
			scale=h/item.height;
		}
		canvas.height=item.height*scale;
		canvas.width=item.width*scale;
		var inner_scale=$obj.height()/item.height;
		canvas.setAttribute('style','width:'+canvas.width/Pixel_Ratio+'px; height:'+canvas.height/Pixel_Ratio+'px;');
		//给相应的作品添加背景
		context.clearRect(0,0,canvas.width,canvas.height);
		context.rect(0,0,canvas.width,canvas.height);
		context.closePath();
		context.fillStyle = '#ffffff';
		context.fill();
		context.closePath();
		context.save();	
		drawToCanvas(canvas,item,context,scale,true);
		var obj={title:item.title,pageid:item.id,imgurl:canvas.toDataURL()};
		UtilProperty[workid].base64image.push(obj);
		var divhtm='<div class="p_canvas_box '+(i==0?"editoring":"")+'" id="'+workid+'_'+item.id+'" title="'+item.title+'" page="'+item.page+'" style=" height:'+item.height*inner_scale+'px;  width:'+item.width*inner_scale+'px;"><img src="'+obj.imgurl+'"></div>'
		$obj.append(divhtm);
		//}
		
		if(opentype==2&&i==0){
			$('.albumitem[product_id="'+product_id+'"][work_id="'+workid+'"] .albumimg').html('<img src="'+obj.imgurl+'"/>');
		}
		if(i==0){
			$(".editor_current").html(item.title!=''?item.title:'第'+item.page+'页');
			if(item.title==''){
				$(".layout_change").removeClass("out_layout_change");
			}
			o_canvas.setAttribute('style','width:'+o_canvas.width/Pixel_Ratio+'px; height:'+o_canvas.height/Pixel_Ratio+'px;');
		}
		
		
		
		
	}
	
	
}


//点击生成大的canvas
function drawToCanvas(canvas,item,context,scale,flag){//flag 是表示没有图片或控件的时候不用显示 +  或者 字等
	for(var k=0; k<item.control_list.length;k++){
			var element=item.control_list[k];
			layout_id=element.layout_id;
			if(element.control_id==1){				
				if(element.element_list instanceof Array&&element.element_list.length>0){									
						var list=element.element_list[0];
						//console.log(item,k, list)
						
						if(UtilProperty[workid].imageObj[list.image_url]){
						var pic=UtilProperty[workid].imageObj[list.image_url];
						var _elementlist={
								imgurl:list.image_url,
								img:pic,
								image_id:list.image_id,
								x: element.coordinate_x*scale,
								y: element.coordinate_y*scale,
								w: element.width*scale,
								h: element.height*scale,
								imgx: list.cut_x*scale+element.coordinate_x*scale,
								imgy: list.cut_y*scale+element.coordinate_y*scale,
								imgw: 0,
								imgh: 0,
								imgoldx: 0,
								imgoldy: 0,
								imgoldw: pic.width,
								imgoldh: pic.height,
								transform:list.transform,								
								}
						if(_elementlist.transform>0) {		
							originalcanvasimgrote(context,_elementlist)
						}else{ 							
							drawImgByStatus(context,_elementlist);	
						}
						
					}
					
						
				}else{
					if(flag){
					var fontcss='bold  x-large Microsoft YaHei';
					//在canvas中增加提示 					
 					createReactTips(context,element,scale,'+',fontcss);
 					}
				}
				
				
			}else if(element.control_id==2){
				if(element.element_list instanceof Array&&element.element_list.length>0&&element.element_list[0].content!=""){						
						var list=element.element_list[0];
						var o={size:50,color:"#000",align:"center"}
						var sty=list.style!=""?JSON.parse(list.style):o;
						context.beginPath();
						context.clearRect(element.coordinate_x*scale, element.coordinate_y*scale, element.width*scale, element.height*scale);
						context.rect(element.coordinate_x*scale, element.coordinate_y*scale, element.width*scale, element.height*scale);
						context.closePath();						
						/*var TextObj={
							x:element.coordinate_x*scale,
							y:element.coordinate_y*scale,
							width:element.width*scale,
							height:element.height*scale,
							family:'Microsoft YaHei',							
							size:Number(sty.size)*scale+"px",
							weight:'',
							color:sty.color.toString(),
							style:'',
							center:sty.align!=''?sty.align.toString():"center",
							text:list.content.toString()
							
						}
						CanvasTextWrap(context,TextObj)*/
						var TextObj=new Object;
						if(parseInt(Number(sty.size)*scale)<12){
							var newscale=12/parseInt(Number(sty.size)*scale);
							TextObj={
								x:parseInt(element.coordinate_x*scale*newscale),
								y:parseInt(element.coordinate_y*scale*newscale),
								width:parseInt(element.width*scale*newscale),
								height:parseInt(element.height*scale*newscale),
								family:'Microsoft YaHei',							
								size:12+"px",
								weight:'',
								color:sty.color.toString(),
								style:'',
								center:sty.align!=''?sty.align.toString():"center",
								lineHeight:parseInt((Number(sty.size)+30)*scale*newscale),
								text:list.content.toString()							
							}
							
						}else{
						 TextObj={
							x:parseInt(element.coordinate_x*scale),
							y:parseInt(element.coordinate_y*scale),
							width:parseInt(element.width*scale),
							height:parseInt(element.height*scale),
							family:'Microsoft YaHei',							
							size:parseInt(Number(sty.size)*scale)+"px",
							weight:'bold',
							color:sty.color.toString(),
							style:'',
							center:sty.align!=''?sty.align.toString():"center",
							lineHeight:parseInt((Number(sty.size)+30)*scale),
							text:list.content.toString()							
							}
						}
						
						drawlinetext(TextObj,function(data){
							context.drawImage(data,element.coordinate_x*scale,element.coordinate_y*scale,element.width*scale, element.height*scale);
							context.save();
							context.clip();	
							context.restore();
						});
							
						
					
				}else{
					if(flag){
					var fontcss='bold  x-large Microsoft YaHei';
					//在canvas中增加提示 					
 					createReactTips(context,element,scale,'字',fontcss);
 					}
				}
			}
			
			
	}
	
	
}

//得到点击的区域
function originalTouchEnd(event){
	event.preventDefault();
	var canvas=document.getElementById('originalcanvas');
	var context=canvas.getContext('2d');
	var _touch=event.changedTouches[0]	
	var x=_touch.clientX-canvas.getBoundingClientRect().left;
	var y=_touch.clientY-canvas.getBoundingClientRect().top;
	var pageno=$("#originalcanvas").attr("page");
	var tempitem=UtilProperty[workid].worker[pageno];	
	var canvasData=tempitem.control_list;
	var w=boxwindthheight.width;
	var h=boxwindthheight.height;
		var o_scale;
		
		/*if(w>h){			
			 o_scale=canvas.height/tempitem.height;
		}else{			
			 o_scale=canvas.width/tempitem.width;
		}*/
		
		if(w>h){			
			 o_scale=canvas.height/Pixel_Ratio/tempitem.height;
		}else{			
			 o_scale=canvas.width/Pixel_Ratio/tempitem.width;
		}
		
	var currentid="";//当前被点击中的控件id;	
	var currentType="";//当前被点击中的控件类型;	
	ctxmsg={}
	for(var i=0; i<canvasData.length;i++){	
		var item=canvasData[i];		
		/*var dx=(item.coordinate_x+item.width)*o_scale-x;
		var dy=(item.coordinate_y+item.height)*o_scale-y;*/
		var dx=(item.coordinate_x+item.width)*o_scale;
		var dy=(item.coordinate_y+item.height)*o_scale;
		var x1=item.coordinate_x*o_scale;
		var y1=item.coordinate_y*o_scale;		
		
		var scale;
			
			if((item.width/item.height)>(w/h)){			
				 scale=w/item.width;
			}else{
				scale=h/item.height;
			}
			if(item.control_id==1&&(item.element_list instanceof Array&&item.element_list.length>0)){
				var list=item.element_list[0];
				var pic=UtilProperty[workid].imageObj[list.image_url];
				ctxmsg[item.id] = {
						imgurl:list.image_url,
						img:pic,
						image_id:list.image_id,
						x: 0,
						y: 0,
						w: item.width*scale,
						h: item.height*scale,
						imgx: list.cut_x*scale,
						imgy: list.cut_y*scale,
						imgw: 0,
						imgh: 0,
						imgoldx: 0,
						imgoldy: 0,
						imgoldw: pic.width,
						imgoldh: pic.height,
						imgwid:list.width,
						imghei:list.height,
						transform:item.element_list[0].transform,						
						scale:scale,						
						ischange:item.ischange,
						disabled:item.disabled
					};
			}else{
				ctxmsg[item.id]=new Object;
				ctxmsg[item.id].disabled=item.disabled;
			}
			if(item.control_id==2&&(item.element_list instanceof Array&&item.element_list.length>0)){
				var list=item.element_list[0];
				var o={size:50,color:"#000",align:"center"}
				var sty=list.style!=""?JSON.parse(list.style):o;
				ctxtext[item.id] = {
						content:list.content,
						id:list.id,	
						count:18,
						family:'Microsoft YaHei',							
						size:Number(sty.size),
						weight:'',
						color:sty.color.toString(),
						style:'',
						align:sty.align!=''?sty.align.toString():"center",						
						transform:0,						
						scale:scale,
						ischange:item.ischange,
						disabled:item.disabled
					};
			}else{
				ctxtext[item.id]=new Object;
				ctxtext[item.id].disabled=item.disabled;
				ctxtext[item.id].count=10;
			}
			
			
			//if(dx>=0&&dy>=0){
			if((x1-x<=0&&dx-x>=0)&&(y1-y<=0&&dy-y>=0)){	
				if(item.control_id==1){
					
					//if(!ctxmsg[currentid].imgurl){
					if(ctxmsg.hasOwnProperty(item.id)){
						currentid=item.id;
						currentType=item.control_id;
						//break;
					}
				}else if(item.control_id==2){				
					
					if(ctxtext.hasOwnProperty(item.id)){
						currentid=item.id;
						currentType=item.control_id;
					//if(!ctxtext[currentid].content){
						//break;
					}
				}
			}
		}
	if(currentType==1){
		if(ctxmsg[currentid].disabled==0&&ctxmsg[currentid].imgurl){
			if(currentid!=""){
				enterDrawbefore(currentid);
			}else{
				$(".dialog_tips").show().find(".dialog_txt").text("请填充完整再编辑");	
				setTimeout(function(){$(".dialog_tips").hide()},1000)
			}
			
		}else if(ctxmsg[currentid].disabled==0){
			$("#singerFiles .upload_img").removeClass("active_border");
			$('.up_page').fadeIn()
			$(".up_pagecontain").animate({bottom: '0'}, "slow");
			$("#singerInput").attr("onfunction","0");
			$("#originalcanvas").attr("currentid",currentid);
			$(".singerInputmask").css("height",$('.up_page').height()-$(".up_pagecontain").height());
		}
	}else if(currentType==2&&ctxtext[item.id].disabled==0){				
		Drawtextopen(currentid,scale);//打开文字编辑区域进行编辑
	}
}

//进入编辑图片页面前执行的操作
function enterDrawbefore(currentid){
	
	$("#editor_control_box").empty();
	for(var i in ctxmsg){
	if(ctxmsg[i].imgurl&&ctxmsg[i].disabled==0){		
		var list=ctxmsg[i]		
			var scale=$("#editor_control_box").height()/list.h;
			var styletxt='width: '+list.h*scale+'px; height: '+list.h*scale+'px; background-image:url('+list.img.src+')';
			var strw=list.img.width>list.img.height?'style="height:100%;"':'style="width:100%;"';		
			var html='<div class="editor_control_list '+(i==currentid?"editoring":"")+'" currentid="'+i+'"   style="'+styletxt+'"></div>';
			$("#editor_control_box").append(html);
		
		
		}
	}
	
	canvas_img.width=ctxmsg[currentid].w;
	canvas_img.height=ctxmsg[currentid].h;
	$('#reallycanvas').closest(".canvasbg").css({"width":ctxmsg[currentid].w/Pixel_Ratio,"height":ctxmsg[currentid].h/Pixel_Ratio});
	canvas_img.setAttribute("currentid",currentid);
	canvas_img.setAttribute('style','width:'+canvas_img.width/Pixel_Ratio+'px; height:'+canvas_img.height/Pixel_Ratio+'px;')
	canvasdrag();
	$(".editor_view").hide();
	$(".editor_oper_con").show();
	
}





//保存图片编辑
function saveimageeditor(){	
		var scale=ctxmsg.scale;
		
		var index= $("#originalcanvas").attr("page");		
		var canvasData=UtilProperty[workid].worker[index].control_list;
		var currentid=canvas_img.getAttribute("currentid");
		for(var i=0; i<canvasData.length;i++){
			for(var k in ctxmsg){
				if(k==canvasData[i].id){	
					if(ctxmsg[k].scale){
					var scale=ctxmsg[k].scale;	
					UtilProperty[workid].worker[index].control_list[i].ischange=ctxmsg[k].ischange;
					UtilProperty[workid].worker[index].control_list[i].element_list[0].cut_x=ctxmsg[k].imgx/scale;
					UtilProperty[workid].worker[index].control_list[i].element_list[0].cut_y=ctxmsg[k].imgy/scale;
					UtilProperty[workid].worker[index].control_list[i].element_list[0].height=ctxmsg[k].imghei;
					UtilProperty[workid].worker[index].control_list[i].element_list[0].image_id=ctxmsg[k].image_id;					
					UtilProperty[workid].worker[index].control_list[i].element_list[0].image_url=ctxmsg[k].imgurl;
					UtilProperty[workid].worker[index].control_list[i].element_list[0].transform=ctxmsg[k].transform;
					UtilProperty[workid].worker[index].control_list[i].element_list[0].width=ctxmsg[k].imgwid;	
					UtilProperty[workid].imageObj[ctxmsg[k].imgurl]=ctxmsg[k].img;
					}
				}
			}
		}
		ctxmsg ={};
		changeoriginalcanvas(index);
		$(".editor_oper_con").hide();
		$(".editor_view").show();
		changepagemainbox(index);
}

CanvasRenderingContext2D.prototype.clear = 
  CanvasRenderingContext2D.prototype.clear || function (preserveTransform) {
    if (preserveTransform) {
      this.save();
      this.setTransform(1, 0, 0, 1, 0, 0);
    }

    this.clearRect(0, 0, this.canvas.width, this.canvas.height);

    if (preserveTransform) {
      this.restore();
    }           
};




//改变大的canvas 的布局及图片
function changeoriginalcanvas(index){
		$("#originaleditor").empty();
		//var canvas= document.getElementById('originalcanvas');
		var canvas= document.createElement("canvas");
			canvas.id="originalcanvas";
			canvas.setAttribute("page",index);
			$("#originaleditor").append(canvas);
		var item=UtilProperty[workid].worker[index];
		var context=canvas.getContext("2d");			
		var w=boxwindthheight.width;
		var h=boxwindthheight.height;		
		var scale;		
		if(item.width / item.height > w / h) {
			scale=w/item.width;
		} else {
			scale=h/item.height;
		}		
		canvas.height=item.height*scale;
		canvas.width=item.width*scale;		
		$("#originaleditor").append(canvas);
		$('#originaleditor').css({"width":canvas.width/Pixel_Ratio,"height":canvas.height/Pixel_Ratio});	
		canvas.setAttribute('style','width:'+canvas.width/Pixel_Ratio+'px; height:'+canvas.height/Pixel_Ratio+'px;');
		//给相应的作品添加背景
		context.clearRect(0,0,canvas.width,canvas.height);
		context.rect(0,0,canvas.width,canvas.height);
		context.closePath();
		context.fillStyle = '#ffffff';
		context.fill();
		context.closePath();
		context.save();	
		drawToCanvas(canvas,item,context,scale,true)
		document.getElementById('originalcanvas').addEventListener("touchend", originalTouchEnd, false);
	 
	
	
}
//改变小的canvas布局 
function changepagemainbox(index){	
	var item = UtilProperty[workid].worker[index];	
	UtilProperty[workid].base64image[index].imgurl=document.getElementById('originalcanvas').toDataURL();
	$("#pagemain .p_canvas_box").eq(index).html('<img src="'+UtilProperty[workid].base64image[index].imgurl+'">');
}


//元素旋转
function originalcanvasimgrote(context,_elementlist){			
		_elementlist.transform = _elementlist.transform % 360;
	
		var w = _elementlist.img.width;
		var h = _elementlist.img.height;
	
		var x = w / -2;
		var y = h / -2;
	
		if(_elementlist.transform % 180 != 0) {
			create_cvs.width = h;
			create_cvs.height = w;
			if(w > h) {
				//console.log(1);
				_elementlist.imgw = _elementlist.w;
				_elementlist.imgh = _elementlist.w / h * w;
			} else {
				//console.log(2);
				_elementlist.imgh = _elementlist.h;
				_elementlist.imgw = _elementlist.h / w * h;
			}
		} else {
			create_cvs.width = w;
			create_cvs.height = h;
			if(w > h) {
				//console.log(3);
				_elementlist.imgh = _elementlist.h;
				_elementlist.imgw = _elementlist.h / h * w;
			} else {
				//console.log(4);
				_elementlist.imgw = _elementlist.w;
				_elementlist.imgh = _elementlist.w / w * h;
			}
		}
	 	create_cvs_2d.clearRect(0, 0, create_cvs_2d.width, create_cvs_2d.height);
		create_cvs_2d.translate(create_cvs.width / 2, create_cvs.height / 2);
		create_cvs_2d.rotate(_elementlist.transform * Math.PI / 180);
		create_cvs_2d.drawImage(_elementlist.img, x, y);
	
		//防止旋转图片后，指定画图区留白（图片小于画图区）  
		if(_elementlist.imgw < _elementlist.w) {
			_elementlist.imgh = _elementlist.w / _elementlist.imgw * _elementlist.imgh;
			_elementlist.imgw = _elementlist.w;
		}
		if(_elementlist.imgh < _elementlist.h) {
			_elementlist.imgw = _elementlist.h / _elementlist.imgh * _elementlist.imgw;
			_elementlist.imgh = _elementlist.h;
		}
	
		/*_elementlist.imgx = _elementlist.x;
		_elementlist.imgy = _elementlist.y;*/
		drawImgByStatus(context,_elementlist)
		
	
		
	};	
	

//绘图
function drawImgByStatus(context,_elementlist){	
	if(_elementlist.image_id==6873){
		console.log(_elementlist.x, _elementlist.y, _elementlist.w, _elementlist.h)
		}
		context.beginPath();
		context.clearRect(_elementlist.x, _elementlist.y, _elementlist.w, _elementlist.h);
		context.rect(_elementlist.x, _elementlist.y, _elementlist.w, _elementlist.h);
		context.save();
		context.clip();		    
		if(!_elementlist.imgw) {
			//设置画入画布内的图片宽、高（等比例铺满指定盒子，溢出被剪切掉）    
			if(_elementlist.imgoldw / _elementlist.imgoldh > _elementlist.w / _elementlist.h) {
				_elementlist.imgh = _elementlist.h;
				_elementlist.imgw = _elementlist.h / _elementlist.imgoldh * _elementlist.imgoldw;
			} else {
				_elementlist.imgw = _elementlist.w;
				_elementlist.imgh = _elementlist.w / _elementlist.imgoldw * _elementlist.imgoldh;
			}
		}
		if(_elementlist.transform!=0) {	
			context.drawImage(create_cvs, _elementlist.imgx, _elementlist.imgy, _elementlist.imgw, _elementlist.imgh);
		} else{			
			context.drawImage(_elementlist.img, _elementlist.imgx, _elementlist.imgy, _elementlist.imgw, _elementlist.imgh);
			
		}
		context.restore();
	};

//添加图片
 function addimgaetocanvas(imgurl,image_id,width,height){
	//imgurl=imgurl.split('?')[0]
	if(UtilProperty[workid].imageObj[imgurl]==undefined){	
		var mulitImg=new Array;		
		mulitImg.push(imgurl);	
		imagelistToObject(mulitImg,workid,function(){
		//打开编辑作品页面并且合成相应的页面
		getnewimage(image_id,imgurl,width,height);
		})
	}else{
		//已经存在的照片 直接 修改 UtilProperty和当前的 相册页
		getnewimage(image_id,imgurl,width,height);
	}
}
//添加图片时修改 UtilProperty和当前的 相册页
function getnewimage(image_id,imgurl,width,height){
		var canvas= document.getElementById('originalcanvas');	
		var pageno=canvas.getAttribute("page");		
		var canvasData=UtilProperty[workid].worker[pageno].control_list;
		var currentid=canvas.getAttribute("currentid");
		var work_page_id=UtilProperty[workid].worker[pageno].id;
		for(var i=0; i<canvasData.length;i++){
			var item = canvasData[i];
			if(item.id==currentid){	
				UtilProperty[workid].worker[pageno].control_list[i].ischange=true;
				UtilProperty[workid].worker[pageno].control_list[i].element_list=new Array;
				var obj={
						id: '',
						work_page_id: work_page_id,
						image_id: image_id,						
						image_url:imgurl,
						width: width,
						height: height,
						cut_x: 0,
						cut_y: 0,
						transform: 0.00,
						style: ""
					}
				if(!item.layout_id){
					obj.work_control_id=item.id;
				}
				UtilProperty[workid].worker[pageno].control_list[i].element_list.push(obj);
			break;
			}
		}
		changeoriginalcanvas(pageno);
		changepagemainbox(pageno);
		$(".up_page").hide();
		
	}



//把canvas保存图片并预览

function saveCanvasOper(){
	UtilProperty[workid].update_work_page=new Array;
	UtilProperty[workid].base64image=new Array;
	var worker=UtilProperty[workid].worker;	
	var canvas=document.createElement("canvas");						
	var context=canvas.getContext("2d");
	var $_this=$(".previewlist_box");
	$_this.empty();
	$(".preview_bigview").empty();
	var larg_html="";
	var inner_html="";	
	var w=boxwindthheight.width;
	var h=boxwindthheight.height;
	var inner_flag=false;
	var update_flag=true;
	var larg_w,larg_h; 
	for(var i=0; i<worker.length;i++){		
		var item=worker[i];	
		//if(item.title!="扉页"){
		var scale;		
		if(item.width / item.height > w / h) {
			scale=w/item.width;
		} else {
			scale=h/item.height;
		}
		canvas.height=item.height*scale;
		canvas.width=item.width*scale;
		larg_w=canvas.width;
		larg_h=canvas.height;
		//var inner_scale=($_this.height()-8)/item.height;
		canvas.setAttribute('style','width:'+canvas.width/Pixel_Ratio+'px; height:'+canvas.height/Pixel_Ratio+'px;');
		//给相应的作品添加背景
		context.clearRect(0,0,canvas.width,canvas.height);
		context.rect(0,0,canvas.width,canvas.height);
		context.closePath();
		context.fillStyle = '#ffffff';
		context.fill();
		context.closePath();
		context.save();	
		
		drawToCanvas(canvas,item,context,scale,true);
		var obj={title:item.title,pageid:item.id,imgurl:canvas.toDataURL()};
		UtilProperty[workid].base64image.push(obj);		
		var inner_scale=$_this.height()/item.height;
		if(item.title=="封面"||item.title=="封底"){
			var pagehtml='<div class="preview_page"><div class="preview_element '+(item.title=="封面"?"focus":"")+' " page="'+item.page+'" title="'+item.title+'" style=" text-align: center; height:'+($_this.height()-8)+'px; width:'+($_this.height()-8)/item.height*item.width+'px;"><img src="'+obj.imgurl+'"></div></div>';
			$_this.append(pagehtml);
			if(item.title=="封面"){
				larg_html='<div class="large_page_view"><img src="'+obj.imgurl+'"></div>';	
			}
			
		}else{
			if(i==0){
				larg_html='<div class="large_page_view"><img src="'+obj.imgurl+'"></div>';	
			}
			if(inner_html!=''){
				inner_flag=true;				
			}
			//inner_html=inner_html+'<div class="preview_element  '+(i==0?"focus":"")+' " page="'+item.page+'" title="'+item.title+'" style="text-align: center; height:'+($_this.height()-8)+'px; width:'+($_this.height()-8)/item.height*item.width+'px; '+(inner_flag==true?"border-left:1px dashed #d8d8d8":"")+'"><img src="'+obj.imgurl+'"></div>';
			inner_html=inner_html+'<div class="preview_element  '+(i==0?"focus":"")+' " page="'+item.page+'" title="'+item.title+'" style="text-align: center; height:'+($_this.height()-8)+'px; width:'+($_this.height()-8)/item.height*item.width+'px;"><img src="'+obj.imgurl+'"></div>';
		
		}
		if(inner_flag){
			var inner_page='<div class="preview_page">'+inner_html+'</div>';
			$_this.append(inner_page);
			inner_html=''
			inner_flag=false;
		}
		context.clearRect(0,0,canvas.width,canvas.height);
		//}
		
		if(i==0){			
			$(".editor_preview").html(item.title!=''?item.title:'第'+item.page+'页');
		}
	}
	
	for(i=0; i<worker.length;i++){		
		var item=worker[i];	
		var updatelist=new Array;
		var layout_id;
		for(k=0; k<item.control_list.length;k++){
			var element=item.control_list[k];
			layout_id=element.layout_id;
			if(element.control_id==1){				
				if(element.ischange||layout_id!=undefined){//lym 2018 01.04 修改内容被清空
					var up_obj=new Object;
					if(element.element_list instanceof Array&&element.element_list.length>0){									
						var list=element.element_list[0];
						up_obj={image_id:list.image_id,width:list.width,height:list.height,cut_x:list.cut_x,cut_y:list.cut_y,transform:list.transform,style:list.style};
					}	
					var updata_obj={source_control_id:element.id,control_id:element.control_id,content:up_obj}							
					updatelist.push(updata_obj);
				}


				
			}else if(element.control_id==2){	
				if(element.ischange||layout_id!=undefined){//lym 2017 11.24 修改内容被清空
					var updata_obj={source_control_id:element.id,control_id:element.control_id}
					if(element.element_list instanceof Array&&element.element_list.length>0){	
						updata_obj.content={content:element.element_list[0].content,style:element.element_list[0].style};
						
					}
												
					updatelist.push(updata_obj);
				}
			}
		
			
		}
		if(updatelist.length>0||item.background.ischange==true){
			var un_obj=new Object;
			un_obj.page_id=item.id;
			un_obj.background_id=item.background_id;
			if(layout_id&&updatelist.length>0){
				un_obj.layout_id=layout_id;			
			}else{
				un_obj.layout_id=0;	
			}
			un_obj.control_list=updatelist;
			
			UtilProperty[workid].update_work_page.push(un_obj);
		}
	
	
	}
	
	$(".preview_bigview").css({"width":larg_w/Pixel_Ratio+"px","height":larg_h/Pixel_Ratio+"px",});
	$(".preview_bigview").append(larg_html);

	
}


//canvas保存图片成功后修改ischange的值
function changeischange(){
	UtilProperty[workid].update_work_page=new Array;	
	var worker=UtilProperty[workid].worker;
	for(i=0; i<worker.length;i++){		
		var item=worker[i];			
		for(k=0; k<item.control_list.length;k++){			
			UtilProperty[workid].worker[i].control_list[k].ischange=false;
		}
		
	
	}
		
}




//在canvas中增加提示 
function createReactTips(context,element,scale,text,fontcss){
	context.beginPath();
	//设置画入画布内的图片宽、高（等比例铺满指定盒子，溢出被剪切掉）    
	context.clearRect(element.coordinate_x * scale, element.coordinate_y * scale, element.width * scale, element.height * scale);
	context.rect(element.coordinate_x * scale, element.coordinate_y * scale, element.width * scale, element.height * scale);
	context.closePath();
	context.fillStyle = 'rgba(67,67,67,.3)';
	context.fill();
	if(element.control_id==1){
	var r = 0;
	if(element.width > element.height) {
		r = element.width * scale / 2 / 2
	} else {
		r = element.height * scale / 2 / 2
	}
	if(r > 20) {
		r = 20
	}
	context.beginPath();
	context.arc(element.coordinate_x * scale + element.width * scale / 2, element.coordinate_y * scale + element.height * scale / 2, r, 0, Math.PI * 2);
	context.strokeStyle = "#878787";
	context.lineWidth = 1;
	context.closePath();
	context.stroke();
	}
	context.beginPath();
	context.fillStyle = '#878787';
	context.textAlign = "center";
	context.textBaseline = "middle";
	context.font = fontcss;
	context.fillText(text, element.coordinate_x * scale + element.width * scale / 2, element.coordinate_y * scale + element.height * scale / 2);
	context.closePath();
	context.save();
	context.clip();
	context.restore();
	
}

//检测作品中所有的控件是否都有填充了图片或文字
function checkwork(){
	var worker=UtilProperty[workid].worker;	
	var isfa=false;	
	for(i=0; i<worker.length;i++){		
		var item=worker[i];		
		for(k=0; k<item.control_list.length;k++){
			var element=item.control_list[k];			
			if(element.control_id==1){				
				if(element.element_list instanceof Array&&element.element_list.length>0){						
					if(!element.element_list[0].image_url){					
						isfa=true
					     return isfa;
					}
				//console.log(i)		
				}else{
					isfa=true
					return isfa;
				}
				
			}else if(element.control_id==2){				
				if(element.element_list instanceof Array&&element.element_list.length>0){						
					if(element.element_list[0].content==''||!element.element_list[0].content){					
						isfa=true
					     return isfa;
					}
				//console.log(i)		
				}else{
					isfa=true
					return isfa;
				}
				
			}
		
			
		}
	
	}
}
// 已提交的作品
function submitworkview(){
	var worker=UtilProperty[workid].worker;	
	UtilProperty[workid].base64image=new Array;
	var canvas=document.createElement("canvas");						
	var context=canvas.getContext("2d");
	var w=boxwindthheight.width;
	var h=boxwindthheight.height;
	var $_this=$(".previewlist_box");
	$_this.empty();
	$(".preview_bigview").empty();
	var larg_html="";
	var inner_html="";	
	var inner_flag=false;
	var update_flag=true;
	for(var i=0; i<worker.length;i++){
		//if(item.title!="扉页"){
		var item=worker[i];	
		var scale;		
		if(item.width / item.height > w / h) {
			scale=w/item.width;
		} else {
			scale=h/item.height;
		}
		canvas.height=item.height*scale;
		canvas.width=item.width*scale;
		//var inner_scale=($_this.height()-8)/item.height;
		canvas.setAttribute('style','width:'+canvas.width/Pixel_Ratio+'px; height:'+canvas.height/Pixel_Ratio+'px;');
		//给相应的作品添加背景
		context.clearRect(0,0,canvas.width,canvas.height);
		context.rect(0,0,canvas.width,canvas.height);
		context.closePath();
		context.fillStyle = '#ffffff';
		context.fill();
		context.closePath();
		context.save();	
		drawToCanvas(canvas,item,context,scale,false);
		var obj={title:item.title,pageid:item.id,imgurl:canvas.toDataURL()};
		UtilProperty[workid].base64image.push(obj);		
		var inner_scale=$_this.height()/item.height;
		if(item.title=="封面"||item.title=="封底"){
			var pagehtml='<div class="preview_page"><div class="preview_element '+(item.title=="封面"?"focus":"")+' " page="'+item.page+'" title="'+item.title+'" style=" text-align: center; height:'+($_this.height()-8)+'px; width:'+($_this.height()-8)/item.height*item.width+'px;"><img src="'+obj.imgurl+'"></div></div>';
			$_this.append(pagehtml);
			if(item.title=="封面"){
				larg_html='<div class="large_page_view"><img src="'+obj.imgurl+'"></div>';	
			}
			
		}else{
			if(i==0){
				larg_html='<div class="large_page_view"><img src="'+obj.imgurl+'"></div>';	
			}
			if(inner_html!=''){
				inner_flag=true;				
			}			
			inner_html=inner_html+'<div class="preview_element '+(i==0?"focus":"")+'" page="'+item.page+'" title="'+item.title+'" style="text-align: center; height:'+($_this.height()-8)+'px; width:'+($_this.height()-8)/item.height*item.width+'px;"><img src="'+obj.imgurl+'"></div>';
		}
		if(inner_flag){
			var inner_page='<div class="preview_page">'+inner_html+'</div>';
			$_this.append(inner_page);
			inner_html=''
			inner_flag=false;
		}
	context.clearRect(0,0,canvas.width,canvas.height);
	//}
	if(i==0){			
			$(".editor_preview").html(item.title!=''?item.title:'第'+item.page+'页');
		}
	}
	//$(".editor_preview").html('封面');
	$(".preview_bigview").css({"width":canvas.width/Pixel_Ratio+"px","height":canvas.height/Pixel_Ratio+"px",});
	$(".preview_bigview").append(larg_html);
	
}

function Drawtextopen(currentid,scale){	
	//if(ctxtext[currentid].size!=''){
	if(ctxtext[currentid].hasOwnProperty('size')){
		var item=ctxtext[currentid];
		var textstyle='font-family:'+item.family+'; font-size:'+item.size*item.scale+'px; color:'+item.color+'; font-weight:'+item.weight+';  text-align:'+item.align;
		$(".text_input").attr({"textstyle":textstyle,"currentid":currentid,"scale":scale,"maxlength":item.count});
		$(".text_input").val(item.content);		
	}else{
		var textstyle='font-family:Microsoft YaHei; font-size:12px; color:#333; font-weight:100; text-align:center';
		$(".text_input").attr({"textstyle":textstyle,"currentid":currentid,"scale":scale,"maxlength":ctxtext[currentid].count});
		$(".text_input").val('');
	}

	$('.editor_textview').fadeIn()
	$(".text_contain").animate({bottom: '0'}, "slow");	
	$("#originalcanvas").attr("currentid",currentid);
	$(".textviewInputmask").css("height",$('.editor_textview').height()-$(".text_contain").height());
	
	
}
//把编辑过的文字回填
 function comfrimTextToCanvas(){
 		var k=$(".text_input").attr("currentid");
 		var scale=$(".text_input").attr("scale");	
 		var stylestr=$(".text_input").attr("textstyle").split("; ");//分割 
 		var sty=new Object;
		//遍历匹配
		for ( var i = 0; i < stylestr.length; i++) {
			var arr = stylestr[i].split(":");
			var newarr=arr[0].split("-");
			sty[newarr[1]==undefined?newarr[0]:newarr[1]]=arr[1];			
		}
		var index= $("#originalcanvas").attr("page");		
			ctxtext[k] = {
				content: $(".text_input").val(),
				id: '',
				count: $(".text_input").attr('maxlength'),
				family: sty.family,
				size: Number(sty.size.split("p")[0]),
				weight: '',
				color: sty.color.toString(),
				style: '',
				align: sty.align,
				transform: 0,
				scale: scale,
				ischange: true
			};
		var canvasData=UtilProperty[workid].worker[index].control_list;		
		for(var i=0; i<canvasData.length;i++){			
				if(k==canvasData[i].id){
					if(!(JSON.stringify(ctxtext[k]) == "{}")){	
						var element=UtilProperty[workid].worker[index].control_list[i];
					if(element.element_list instanceof Array&&element.element_list.length==0){
						UtilProperty[workid].worker[index].control_list[i].element_list[0]=new Object;
					}
					
					var fontstyle={size:parseInt(ctxtext[k].size/scale),color:ctxtext[k].color,align:ctxtext[k].align};
					UtilProperty[workid].worker[index].control_list[i].ischange=ctxtext[k].ischange;
					UtilProperty[workid].worker[index].control_list[i].element_list[0].content=ctxtext[k].content;					
					UtilProperty[workid].worker[index].control_list[i].element_list[0].height=0;
					UtilProperty[workid].worker[index].control_list[i].element_list[0].id=ctxtext[k].id;
					UtilProperty[workid].worker[index].control_list[i].element_list[0].style=JSON.stringify(fontstyle);									
					UtilProperty[workid].worker[index].control_list[i].element_list[0].width=0;									
					UtilProperty[workid].worker[index].control_list[i].element_list[0].work_page_id=UtilProperty[workid].worker[index].id;	
					}
				}
		
		}
		ctxtext ={};	
		changeoriginalcanvas(index);		
		changepagemainbox(index);
 		$('.editor_textview').fadeOut()
		$(".text_contain").animate({bottom: '0'}, "slow");		
		$(".textviewInputmask").css("height",$('.editor_textview').height()-$(".text_contain").height());
 	
 }

//确定更改布局
function comfirmchangelayout(pageno,index){
	var lay_list = UtilProperty[workid].lay_list[index];	
	var temp = UtilProperty[workid].worker[pageno].control_list;
	var pageTempdata = new Array;	
	var work_page_id = UtilProperty[workid].worker[pageno].id;
	for(var i = 0; i < lay_list.control_list.length; i++) {
		var item = lay_list.control_list[i];
		var obj = {
			content: item.content,
			control_id: item.control_id,
			coordinate_x: item.coordinate_x,
			coordinate_y: item.coordinate_y,
			disabled:item.disabled,
			height: item.height,
			id: item.id,
			layout_id: item.layout_id,
			style: item.style,
			transform: item.transform,
			width: item.width,
			z_index: item.z_index			
		};
		obj.work_id = workid;
		obj.work_page_id = work_page_id;
		obj.element_list=new Array;
		obj.ischange = true;
		for(var k = 0; k < temp.length; k++) {
			if(item.control_id == temp[k].control_id) {
				if(temp[k].element_list instanceof Array && temp[k].element_list.length > 0) {
					obj.element_list = temp[k].element_list;
					obj.element_list[0].id = '';
					delete obj.element_list[0].work_control_id;
				}
				temp.splice(k, 1);
				break;
			}
		}
		pageTempdata.push(obj);
	
	}
	UtilProperty[workid].worker[pageno].control_list = pageTempdata;
	changeoriginalcanvas(pageno);
	changepagemainbox(pageno);
	$('.layoutcon').fadeOut()
	$(".layoutbox").animate({bottom: '-100%'}, "slow");
}
//判断选择的布局中的图片控件是否少于当前页面中的图片
function judelayimg(pageno,index){
	var lay_list = UtilProperty[workid].lay_list[index];	
	var temp = UtilProperty[workid].worker[pageno].control_list;
	var pageTempdata = new Array;	
	var work_page_id = UtilProperty[workid].worker[pageno].id;
	var laynum=0;
	var crrentnum=0;
	for(var i = 0; i < lay_list.control_list.length; i++) {
		var item = lay_list.control_list[i];
		if(item.control_id==1){
			laynum++;
		}
	}
	for(var k = 0; k < temp.length; k++) {
			if(temp[k].control_id==1) {
				if(temp[k].element_list instanceof Array && temp[k].element_list.length > 0) {
					if(temp[k].element_list.image_url!=""){
					crrentnum++;	
					}
				}
				
			}
	}
	return crrentnum>laynum?true:false;
	
}
